package com.laboratory.util.db;

import com.laboratory.util.DateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @belongProject: hibernate
 * @belongPackage: com.tjetc.utils
 * @author:xujirong  ,Generator:生成器Code：代码
 * @dscription: 代码生成器工具类-用于生成Entity实体类，
 * service，mapper，controller，生成sql语句，xml文件等
 * @date:2022-10-08 21:18
 * @version: 1.0
 */
public class GeneratorCodeUtils {

    /**2003- 版本的excel*/
    private final static String excel2003L = ".xls";
    /**2007+ 版本的excel*/
    private final static String excel2007U = ".xlsx";

    /***/
    private static String fileName = "数据源-字段列表.xlsx";

    /**数据源*/
    public static String sourceExcel = "C:\\Users\\ASUS\\Desktop\\Java开发数据源字段\\数据源-字段列表.xlsx";

    /**包路径*/
    public static String packagePath = "com.tjetc.";

    /**
     * 实体类名称，根据实体类对象名称创建对应的数据层
     * 例如：UserEntity,AdminEntity
     * */
    public static String entityName = "UserEntity";

    /**初始化*/
    public static String initEntity = "init"+entityName;
    /**新增*/
    public static String insertEntity = "insert"+entityName;
    /**查询所有*/
    public static String queryAllEntityList = "queryAll"+entityName+"List";
    /**条件查询*/
    public static String queryEntityConList = "query"+entityName+"ConList";
    /**根据id查询*/
    public static String queryEntityById = "query"+entityName+"ById";
    /**根据id修改*/
    public static String updEntityById = "upd"+entityName+"ById";
    /**根据id删除*/
    public static String delEntityById = "del"+entityName+"ById";

    public static String entityLower = entityName.substring(0,1).toLowerCase()+entityName.substring(1)+"";


    public static void main(String[] args) {
        System.out.println("getDataSource() = " + getDataSource());


    }


    /**
     *@descriprion: 解析数据源文件Excel，获取数据字段
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static List<Map<String,String>> getDataSource(){
        List<Map<String,String>> dataList = new ArrayList<>();

        try {
            Workbook wb = null;

            //获取后缀名
            String fileType = fileName.substring(fileName.lastIndexOf("."));

            InputStream inStr = new FileInputStream(sourceExcel);


            //对2003版本的Excel的支持 ---- HSSFWorkbook
            if (excel2003L.equals(fileType)) {
                //2003-
                wb = new HSSFWorkbook(inStr);
                //对2007版本以及更高版本的支持 ---- XSSFWorkbook
            } else if (excel2007U.equals(fileType)) {
                // 2007+
                wb = new XSSFWorkbook(inStr);
            } else {
                throw new Exception("解析的文件格式有误！");
            }

            //根据sheet页名称，获取sheet页对象
            //Sheet sheet = wb.getSheetAt(0);
            Sheet sheet = wb.getSheet("User");
            //获取首行-标题行
            Row sheetRow = sheet.getRow(0);

            //除首行外的有效数据行
            int lastRowNum = sheet.getLastRowNum();

            //有效列数
            int numberOfCells = sheetRow.getPhysicalNumberOfCells();

            for (int i = 1; i < (lastRowNum+1); i++) {
                Map<String,String> stringMap = new HashMap<>(16);
                for (int j = 0; j < numberOfCells; j++) {
                    Cell cell = sheet.getRow(i).getCell(j);
                    String stringCellValue = cell.getStringCellValue();
                    stringMap.put("cell"+j,stringCellValue);
                }
                dataList.add(stringMap);
            }

            generateEntityJava(dataList);
            generateControllerJava();
            generateEntityXml(dataList);
            generateEntitySql(dataList);
            generateServiceJava();
            generateServiceImplJava();
            generateMapperJava();

            return dataList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *@descriprion: 生成Entity实体类-若使用lombok依赖，则无需生成get和set方法
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static  void generateEntityJava(List<Map<String,String>> dataList){


        File fileDomain = shareMkdirs(entityName+".java");

        try {

            //使用io字符流进行写文件
            FileWriter fileWriter = new FileWriter(fileDomain);

            fileWriter.write("package "+packagePath+"domain;\n\n");
            fileWriter.write("import java.util.*;\n");

            fileWriter.write("/**\n" +
                    " * @belongProject:hibernate\n" +
                    " * @belongPackage:"+packagePath+"domain\n" +
                    " * @author:xujirong\n" +
                    " * @dscription:"+entityName+"实体类\n" +
                    " * @date: "+DateUtil.currentDateTime() +"\n" +
                    " * @version:1.0\n" +
                    " */\n");


            fileWriter.write("public class "+entityName+" {\n");

            fileWriter.write("\n");
            for (Map<String, String> map : dataList) {
                fileWriter.write("\n");
                fileWriter.write("    /**"+map.get("cell1")+"，length:"+map.get("cell3")+"*/\n");
                fileWriter.write("    private "+map.get("cell2")+" "+map.get("cell0")+";\n\n");
            }

            fileWriter.write("    /**构建无参构造方法*/\n");
            fileWriter.write("    public "+entityName+"(){}\n\n");

            fileWriter.write("    /**构建全参构造方法*/\n");
            fileWriter.write("    public "+entityName+"(");
            for (int i = 0; i < dataList.size(); i++) {
                if (i<(dataList.size()-1)){
                    fileWriter.write(dataList.get(i).get("cell2")+" "+dataList.get(i).get("cell0")+",");
                }else {
                    fileWriter.write(dataList.get(i).get("cell2")+" "+dataList.get(i).get("cell0"));
                }
            }

            fileWriter.write("){\n");

            for (Map<String, String> map : dataList) {
                fileWriter.write("        this."+map.get("cell0")+" = "+map.get("cell0")+";\n");
            }
            fileWriter.write("    }\n\n");


            fileWriter.write("    /**构建get,set方法*/\n");
            for (Map<String, String> map : dataList) {

                fileWriter.write("\n");

                String attributeName = map.get("cell0");
                StringBuffer sb = new StringBuffer(16);
                char[] chars = attributeName.toCharArray();
                for (int i = 0; i < chars.length; i++) {
                    if (i==0){
                        sb.append(String.valueOf(chars[i]).toUpperCase());
                    }else {
                        sb.append(chars[i]);
                    }
                }

                fileWriter.write("    public "+map.get("cell2")+" get"+sb.toString()+"(){\n");
                fileWriter.write("        return "+attributeName+";\n    }\n");

                fileWriter.write("    public void set"+sb.toString()+"("+map.get("cell2")+" "+attributeName+"){\n");
                fileWriter.write("        this."+attributeName+" = "+attributeName+";\n    }\n");

            }

            fileWriter.write("    @Override\n");
            fileWriter.write("    public String toString() {\n");

            fileWriter.write("        return \""+entityName+"{\"+\n");

            for (int i = 0; i < dataList.size(); i++) {
                if (i<(dataList.size()-1)){
                    fileWriter.write("                \""+dataList.get(i).get("cell0")+"=\"+"+dataList.get(i).get("cell0")+"+\",\"+\n");
                }else {
                    fileWriter.write("                \""+dataList.get(i).get("cell0")+"=\"+"+dataList.get(i).get("cell0")+"+\",\"+\n");
                }
            }
            fileWriter.write("                \"}\";\n");
            fileWriter.write("    }\n");
            fileWriter.write("}");

            writerClose(fileWriter);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     *@descriprion: 生成ControllerJava
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static  void generateControllerJava(){

        try {
            File controllerFile = shareMkdirs(entityName+"Controller.java");

            FileWriter fileWriter = new FileWriter(controllerFile);

            fileWriter.write("package "+packagePath+"controller;\n\n");

            fileWriter.write("import "+packagePath+"domain."+entityName+";\n");
            fileWriter.write("import "+packagePath+"service."+entityName+"Service;\n");
            fileWriter.write("import lombok.extern.slf4j.Slf4j;\n");
            fileWriter.write("import org.springframework.stereotype.Controller;\n");
            fileWriter.write("import org.springframework.web.bind.annotation.RequestMapping;\n");
            fileWriter.write("import org.springframework.beans.factory.annotation.Autowired;\n");
            fileWriter.write("import org.springframework.web.bind.annotation.ResponseBody;\n\n");
            fileWriter.write("import java.util.*;\n\n");

            fileWriter.write("/**\n" +
                    " * @belongProject:hibernate\n" +
                    " * @belongPackage:"+packagePath+"controller\n" +
                    " * @author:xujirong\n" +
                    " * @dscription:"+entityName+"Controller控制层\n" +
                    " * @date: "+DateUtil.currentDateTime() +"\n" +
                    " * @version:1.0\n" +
                    " */\n");

            fileWriter.write("@Slf4j\n");
            fileWriter.write("@Controller\n");

            fileWriter.write("@RequestMapping(\"/"+(entityName.substring(0,1).toLowerCase())+entityName.substring(1)+"Controller\")\n");
            fileWriter.write("public class "+entityName+"Controller{\n\n");

            fileWriter.write("    @Autowired\n");
            fileWriter.write("    private "+entityName+"Service "+entityLower+"Service;\n\n");

            fileWriter.write("    /**\n" +
                    "    *@descriprion: 初始化页面\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: String" +
                    "    */\n");
            fileWriter.write("    @GetMapping(\"/"+initEntity+"\")\n");
            fileWriter.write("    public String "+initEntity+"(){\n");
            fileWriter.write("        return \""+(entityName.substring(0,1).toLowerCase())+entityName.substring(1)+"\";\n");
            fileWriter.write("    }\n\n");


            fileWriter.write("    /**\n" +
                    "    *@descriprion: 查询全部数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: List<Map<String,Object>>\n" +
                    "    */\n");
            fileWriter.write("    @RequestMapping(\"/"+queryAllEntityList+"\")\n");
            fileWriter.write("    @ResponseBody\n");
            fileWriter.write("    public List<Map<String,Object>> "+queryAllEntityList+"(){\n");
            fileWriter.write("        return "+entityLower+"Service."+queryAllEntityList+"();\n");
            fileWriter.write("    }\n\n");


            fileWriter.write("    /**\n" +
                    "    *@descriprion: 根据条件查询数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: List<Map<String,Object>>\n" +
                    "    */\n");
            fileWriter.write("    @RequestMapping(\"/"+queryEntityConList+"\")\n");
            fileWriter.write("    @ResponseBody\n");
            fileWriter.write("    public List<Map<String,Object>> "+queryEntityConList+"(Map<String,Object> reqMap){\n");
            fileWriter.write("        return "+entityLower+"Service."+queryEntityConList+"(reqMap);\n");
            fileWriter.write("    }\n\n");


            fileWriter.write("    /**\n" +
                    "    *@descriprion: 根据id查询数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: "+entityName+"\n" +
                    "    */\n");
            fileWriter.write("    @RequestMapping(\"/"+queryEntityById+"\")\n");
            fileWriter.write("    @ResponseBody\n");
            fileWriter.write("    public "+entityName+" "+queryEntityById+"(String id){\n");
            fileWriter.write("        return "+entityLower+"Service."+queryEntityById+"(id);\n");
            fileWriter.write("    }\n\n");


            fileWriter.write("    /**\n" +
                    "    *@descriprion: 根据id修改数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: Map<String,Object>\n" +
                    "    */\n");
            fileWriter.write("    @RequestMapping(\"/"+updEntityById+"\")\n");
            fileWriter.write("    @ResponseBody\n");
            fileWriter.write("    public Map<String,Object> "+updEntityById+"(Map<String,Object> objMap){\n");
            fileWriter.write("        return "+entityLower+"Service."+updEntityById+"(objMap);\n");
            fileWriter.write("    }\n\n");



            fileWriter.write("    /**\n" +
                    "    *@descriprion: 根据id删除数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: Map<String,Object>\n" +
                    "    */\n");
            fileWriter.write("    @RequestMapping(\"/"+delEntityById+"\")\n");
            fileWriter.write("    @ResponseBody\n");
            fileWriter.write("    public Map<String,Object> "+delEntityById+"(String id){\n");
            fileWriter.write("        return "+entityLower+"Service."+delEntityById+"(id);\n");
            fileWriter.write("    }\n\n");



            fileWriter.write("}");


            //fileWriter调用公共方法
            writerClose(fileWriter);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     *@descriprion: 生成Service接口和实现类ServiceImpl
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static  void generateServiceJava(){

        try {
            File serviceFile = shareMkdirs(entityName+"Service.java");

            Writer writer = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(serviceFile), "UTF-8"));
            writer.write("package "+packagePath+"service;\n\n");
            writer.write("import "+packagePath+"domain."+entityName+";\n");
            writer.write("import java.util.*;\n\n");
            writer.write("/**\n" +
                    " * @belongProject:hibernate\n" +
                    " * @belongPackage:"+packagePath+"service\n" +
                    " * @author:xujirong\n" +
                    " * @dscription:"+entityName+"Service业务接口层\n" +
                    " * @date: "+DateUtil.currentDateTime() +"\n" +
                    " * @version:1.0\n" +
                    " */\n");

            writer.write("public interface "+entityName+"Service {\n\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 查询全部数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: List<Map<String,Object>>\n" +
                    "    */\n");
            writer.write("    List<Map<String,Object>> "+queryAllEntityList+"();\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据条件查询数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+ DateUtil.currentDateTime() +"\n\n" +
                    "    *@param:  reqMap\n" +
                    "    *@return: List<Map<String,Object>>\n" +
                    "    */\n");
            writer.write("    List<Map<String,Object>> "+queryEntityConList+"(Map<String,Object> reqMap);\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据id查询数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@param:  id\n" +
                    "    *@return: "+entityName+"\n" +
                    "    */\n");
            writer.write("    "+entityName+" "+queryEntityById+"(String id);\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据id修改数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@param:  objMap\n" +
                    "    *@return: Map<String,Object>\n" +
                    "    */\n");
            writer.write("    Map<String,Object>  "+updEntityById+"(Map<String,Object> objMap);\n\n");



            writer.write("    /**\n" +
                    "    *@descriprion: 根据id删除数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@param:  id\n" +
                    "    *@return: Map<String,Object>\n" +
                    "    */\n");
            writer.write("    Map<String,Object> "+delEntityById+"(String id);\n\n");

            writer.write("}");
            writer.write("");
            writer.write("");

            writer.flush();
            writer.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     *@descriprion: 生成Service接口和实现类ServiceImpl
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static  void generateServiceImplJava(){

        try {
            File serviceFile = shareMkdirs(entityName+"ServiceImpl.java");

            Writer writer = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(serviceFile), "UTF-8"));
            writer.write("package "+packagePath+"service.impl;\n\n");
            writer.write("import "+packagePath+"domain."+entityName+";\n");
            writer.write("import "+packagePath+"service."+entityName+"Service;\n");
            writer.write("import com.tjetc.mapper.UserEntityMapper;\n");
            writer.write("import org.springframework.stereotype.Service;\n");
            writer.write("import lombok.extern.slf4j.Slf4j;\n");
            writer.write("import javax.annotation.Resource;\n");
            writer.write("import java.util.*;\n\n");
            writer.write("/**\n" +
                    " * @belongProject:hibernate\n" +
                    " * @belongPackage:"+packagePath+"serviceImpl\n" +
                    " * @author:xujirong\n" +
                    " * @dscription:"+entityName+"ServiceImpl业务接口实现层\n" +
                    " * @date: "+DateUtil.currentDateTime() +"\n" +
                    " * @version:1.0\n" +
                    " */\n");

            writer.write("@Slf4j\n");
            writer.write("@Service\n");
            writer.write("public class "+entityName+"ServiceImpl  implements "+entityName+"Service {\n\n\n");

            writer.write("    @Resource\n");
            writer.write("    private "+entityName+"Mapper "+entityLower+"Mapper;\n");

            writer.write("    /**\n" +
                    "    *@descriprion: 查询全部数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: List<Map<String,Object>>\n" +
                    "    */\n");
            writer.write("    @Override\n");
            writer.write("    public List<Map<String,Object>> "+queryAllEntityList+"(){\n");
            writer.write("        return "+entityLower+"Mapper."+queryAllEntityList+"();\n");
            writer.write("    }\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据条件查询数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: List<Map<String,Object>>\n" +
                    "    */\n");
            writer.write("    @Override\n");
            writer.write("    public List<Map<String,Object>> "+queryEntityConList+"(Map<String,Object> reqMap){\n");
            writer.write("        return "+entityLower+"Mapper."+queryEntityConList+"(reqMap);\n");
            writer.write("    }\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据id查询数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: "+entityName+"\n" +
                    "    */\n");
            writer.write("    @Override\n");
            writer.write("    public "+entityName+" "+queryEntityById+"(String id){\n");
            writer.write("        return "+entityLower+"Mapper."+queryEntityById+"(id);\n");
            writer.write("    }\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据id修改数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: Map<String,Object>\n" +
                    "    */\n");
            writer.write("    @Override\n");
            writer.write("    public Map<String,Object> "+updEntityById+"(Map<String,Object> objMap){\n");
            writer.write("        return "+entityLower+"Mapper."+updEntityById+"(objMap);\n");
            writer.write("    }\n\n");



            writer.write("    /**\n" +
                    "    *@descriprion: 根据id删除数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: Map<String,Object>\n" +
                    "    */\n");
            writer.write("    @Override\n");
            writer.write("    public Map<String,Object> "+delEntityById+"(String id){\n");
            writer.write("        return "+entityLower+"Mapper."+delEntityById+"(id);\n");
            writer.write("    }\n\n");


            writer.write("}");

            writer.flush();
            writer.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    /**
     *@descriprion: 生成MapperJava
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static  void generateMapperJava(){
        try {
            File serviceFile = shareMkdirs(entityName+"Mapper.java");

            Writer writer = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(serviceFile), "UTF-8"));
            writer.write("package "+packagePath+"mapper;\n\n");
            writer.write("import "+packagePath+"domain."+entityName+";\n");
            writer.write("import org.apache.ibatis.annotations.Mapper;\n");
            writer.write("import org.springframework.stereotype.Repository;\n");
            writer.write("import java.util.*;\n\n");
            writer.write("/**\n" +
                    " * @belongProject:hibernate\n" +
                    " * @belongPackage:"+packagePath+"mapper\n" +
                    " * @author:xujirong\n" +
                    " * @dscription:"+entityName+"Mapper数据层\n" +
                    " * @date: "+DateUtil.currentDateTime() +"\n" +
                    " * @version:1.0\n" +
                    " */\n");

            writer.write("@Repository\n");
            writer.write("@Mapper\n");
            writer.write("public interface "+entityName+"Mapper {\n\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 查询全部数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@return: List<Map<String,Object>>\n" +
                    "    */\n");
            writer.write("    List<Map<String,Object>> "+queryAllEntityList+"();\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据条件查询数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@param:  reqMap\n" +
                    "    *@return: List<Map<String,Object>>\n" +
                    "    */\n");
            writer.write("    List<Map<String,Object>> "+queryEntityConList+"(Map<String,Object> reqMap);\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据id查询数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@param:  id\n" +
                    "    *@return: "+entityName+"\n" +
                    "    */\n");
            writer.write("    "+entityName+" "+queryEntityById+"(String id);\n\n");


            writer.write("    /**\n" +
                    "    *@descriprion: 根据id修改数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@param:  objMap\n" +
                    "    *@return: Map<String,Object>\n" +
                    "    */\n");
            writer.write("    Map<String,Object>  "+updEntityById+"(Map<String,Object> objMap);\n\n");



            writer.write("    /**\n" +
                    "    *@descriprion: 根据id删除数据\n" +
                    "    *@author: xujirong\n" +
                    "    *@date: "+DateUtil.currentDateTime() +"\n" +
                    "    *@param:  id\n" +
                    "    *@return: Map<String,Object>\n" +
                    "    */\n");
            writer.write("    Map<String,Object> "+delEntityById+"(String id);\n\n");

            writer.write("}");
            writer.write("");
            writer.write("");

            writer.flush();
            writer.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *@descriprion: xml文件
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static  void generateEntityXml(List<Map<String,String>> dataList){
        //根据实体类名称-获取表名
        String tabName = "t"+xX2x_x(entityName);
        try {
            File xmlFile = shareMkdirs(entityName+"Mapper.xml");


            Writer fileWriter = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(xmlFile), "UTF-8"));


            fileWriter.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
            fileWriter.write("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\n");
            fileWriter.write("<mapper namespace=\""+packagePath+"mapper."+entityName+"Mapper\">\n\n");

            //新增
            fileWriter.write("    <!--新增-->\n");
            fileWriter.write("    <insert id=\""+insertEntity+"\" parameterType=\""+entityName+"\">\n");

            fileWriter.write("        insert into "+tabName+" (");

            for (int i = 0; i < dataList.size(); i++) {
                if (i==dataList.size()-1){
                    fileWriter.write(dataList.get(i).get("cell0"));
                }else {
                    fileWriter.write(dataList.get(i).get("cell0")+",");
                }
            }

            fileWriter.write(")");
            fileWriter.write(" values (");
            for (int i = 0; i < dataList.size(); i++) {
                if (i==dataList.size()-1){
                    fileWriter.write("#{"+dataList.get(i).get("cell0")+"}");
                }else {
                    fileWriter.write("#{"+dataList.get(i).get("cell0")+"},");
                }
            }
            fileWriter.write(")\n");
            fileWriter.write("    </insert>\n\n");

            fileWriter.write("    <!--查询所有-->\n");
            fileWriter.write("    <select id=\""+queryAllEntityList+"\" resultType=\"java.util.Map\">\n");
            fileWriter.write("        select * from "+tabName+";\n");
            fileWriter.write("    </select>\n\n");

            //查询
            fileWriter.write("    <!--根据条件进行模糊查询-->\n");
            fileWriter.write("    <select id=\""+queryEntityConList+"\" parameterType=\"map\" resultType=\"java.util.Map\">\n");
            fileWriter.write("        select * from "+tabName+"\n");
            fileWriter.write("        <where>\n");
            for (Map<String, String> map : dataList) {

                String attributeName = map.get("cell0");
                fileWriter.write("            <if test=\""+attributeName+" != null and "+attributeName+"!=''\">\n");
                fileWriter.write("                and "+attributeName+" like concat('%',#{"+attributeName+"},'%')\n");
                fileWriter.write("            </if>\n");
            }
            fileWriter.write("        </where>\n");
            fileWriter.write("    </select>\n\n");


            //根据id查询数据
            fileWriter.write("    <!--根据id查询-->\n");
            fileWriter.write("    <select id=\""+queryEntityById+"\" parameterType=\"string\" resultType=\""+entityName+"\">\n");
            fileWriter.write("        select * from "+tabName+" where id=#{id}\n");
            fileWriter.write("    </select>\n\n");

            //根据id修改
            fileWriter.write("    <!--根据id修改-->\n");
            fileWriter.write("    <update id=\""+updEntityById+"\" parameterType=\""+entityName+"\">\n");
            fileWriter.write("        update "+tabName+" \n");
            fileWriter.write("        <set>\n");

            for (int i = 0; i < dataList.size(); i++) {
                if (!"id".equals(dataList.get(i).get("cell0"))){
                    fileWriter.write("            <if test=\""+dataList.get(i).get("cell0")+" != null and "+dataList.get(i).get("cell0")+" != ''\">\n");
                    fileWriter.write("                "+dataList.get(i).get("cell0")+" = #{"+dataList.get(i).get("cell0")+"},\n");
                    fileWriter.write("            </if>\n");
                }

            }
            fileWriter.write("        </set>\n");
            fileWriter.write("        where id=#{"+dataList.get(0).get("cell0")+"}\n");
            fileWriter.write("    </update>\n\n");



            //根据id删除数据
            fileWriter.write("    <!--根据id删除-->\n");
            fileWriter.write("    <delete id=\""+delEntityById+"\" parameterType=\"string\">\n");
            fileWriter.write("        delete from "+tabName+" where id=#{id}\n");
            fileWriter.write("    </delete>\n");


            fileWriter.write("\n");
            fileWriter.write("</mapper>\n");

            fileWriter.flush();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     *@descriprion: 生成Sql脚本文件
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static void generateEntitySql(List<Map<String,String>> dataList){
        //根据实体类名称-获取表名
        String tabName = "t"+xX2x_x(entityName);
        System.out.println("dataList = " + dataList);
        try {
            File xmlFile = shareMkdirs(tabName+".sql");

            Writer writer = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(xmlFile), "UTF-8"));

            writer.write("CREATE TABLE '"+tabName+"' (\n");
            for (int i = 0; i < dataList.size(); i++) {
                System.out.println("dataList.get("+i+") = " + dataList.get(i));
                if (i==0){
                    writer.write("  '"+dataList.get(i).get("cell0")+"' "+dataList.get(i).get("cell2")+" NOT NULL AUTO_INCREMENT,\n");
                }else{
                    //数据类型不为date
                    if (!"date".equals(dataList.get(i).get("cell2"))){
                        String dataType = dataList.get(i).get("cell2");
                        //字符串类型
                        if ("String".equals(dataType) || "string".equals(dataType) || "S".equals(dataType)  || "s".equals(dataType)){
                            writer.write("  '"+dataList.get(i).get("cell0")+"' varchar("+dataList.get(i).get("cell3")+") CHARACTER SET utf8mb4 COLLATE utf8mb4_vietnamese_ci DEFAULT NULL,\n");
                            //浮点型
                        } else if ("double".equals(dataType)){
                            writer.write("  'price' double DEFAULT NULL,");
                        }else {
                            //整形
                            writer.write("  '"+dataList.get(i).get("cell0")+"' int("+dataList.get(i).get("cell3")+") CHARACTER SET utf8mb4 COLLATE utf8mb4_vietnamese_ci DEFAULT NULL,\n");
                        }
                    }else {
                        writer.write("  '"+dataList.get(i).get("cell0")+"' datetime DEFAULT NULL,\n");
                    }
                }
            }
            writer.write("  PRIMARY KEY ('"+dataList.get(0).get("cell0")+"')\n");

            writer.write(") ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_zh_0900_as_cs;\n");
            writer.flush();
            writer.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author Howe
     * @Description 将驼峰转为下划线
     * @param str
     * @return java.lang.String
     * @Date   2022/4/22 13:11
     * @since  1.0.0
     */
    public static String xX2x_x(String str) {
        Pattern compile = Pattern.compile("[A-Z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            matcher.appendReplacement(sb,  "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @author Howe
     * @Description 将下划线转为驼峰
     * @param str
     * @return java.lang.String
     * @Date   2022/4/22 13:12
     * @since  1.0.0
     */
    public static String x_x2xX(String str) {
        str = str.toLowerCase();
        Pattern compile = Pattern.compile("_[a-z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            matcher.appendReplacement(sb,  matcher.group(0).toUpperCase().replace("_",""));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     *@descriprion: 当前用户桌面路径方法
     *@author: xujirong
     *@date: 2022/10/8 21:28
     *@return:
     */
    public static  String getDesktopDirPath(){
        //当前用户桌面路径
        File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        return desktopPath;
    }

    /**
     *@descriprion: 描述
     *@author: xujirong
     *@date: 2022/10/14 21:12
     *@return:
     */
    public static File shareMkdirs(String className){
        try {
            String desktopDirPath = getDesktopDirPath();

            File domainMkdirs = new File(desktopDirPath+"\\testGenerator\\user\\");

            if (!domainMkdirs.exists()) {
                domainMkdirs.mkdirs();
            }


            File fileDomain = new File(domainMkdirs,className);
            if (!fileDomain.exists()){
                fileDomain.createNewFile();
            }
            return fileDomain;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void writerClose(FileWriter fileWriter){
        try {
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
